/**
 * create by hp
 * date 2020/3/7 12:30
 * @type {string[]}
 */

import {Client} from 'ssh2'
import {sshEmitter} from './SSHEmitter'

type option = {
  host: string,
  port: 22,
  username: string,
  password: string
}

/**
 * ssh 连接工具类
 * @param options
 */
export function sshHelper(options: option, flag: String): void {
  const conn = new Client()
  conn.on('ready', async () => {
    sshEmitter.emit("ready", conn, flag)
  }).on('error', (err) => {
    sshEmitter.emit("error", err, flag)
  }).on('end', () => {
    sshEmitter.emit("end", flag)
  }).on('close', (had_error) => {
    sshEmitter.emit("close", had_error, flag)
  }).connect(options)
}

/**
 * ssh 连接状态
 */
export enum SSHStateEnum {
  READY, ERROR, END, CLOSE
}
